package com.mouse.remind;


import java.util.Calendar;

import com.mouse.remind.AlarmModel.Repeatday;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.util.Log;

public class AlarmUpdateReceiver extends BroadcastReceiver {
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(this.getClass().getSimpleName(),"onReceive()");
		
		setServiceAlarm(context, 0);
        	
        
	}
	
	public void setServiceAlarm(Context context, int i) {
		
		Calendar mCalendar;
		String mFormat;
		//String M12 = "hh:mm";
		String M24 = "kk:mm";
		mCalendar = Calendar.getInstance();
		mCalendar.setTimeInMillis(System.currentTimeMillis());
		mFormat = M24;
		CharSequence newTime = DateFormat.format(mFormat, mCalendar);
		String currentTime = newTime.toString();
		String [] temp = currentTime.split(":");
		int hourValue = Integer.parseInt(temp[0]);
		hourValue ++;
		if (hourValue >= 24) {
			hourValue = 0;
		}
		if (hourValue < 10) {
			currentTime = "0" + hourValue + ":" + temp[1];
		} else {
			currentTime = hourValue + ":" + temp[1];
		}
		
		Log.v("mouse", currentTime);
		AlarmModel alarm = new AlarmModel();
		alarm.setTime(currentTime);
		alarm.setRepeatday(Repeatday.ONECE_OF_DAY);
		Intent mIntent = new Intent(context, AlarmUpdateReceiver.class);
		mIntent.putExtra("alarm", alarm);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mIntent,PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.getAlarmTime().getTimeInMillis(), pendingIntent);	
		
		/*String action = intent.getAction();
        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {*/
            //重新计算闹铃时间，并调设置闹铃时间及闹铃间隔时间
        Intent intent = new Intent(context, AlarmUpdateService.class);
        context.startService(intent); 
       //}
	}

}
